package org.carabiner.infinitest.swingui;

import javax.swing.SwingUtilities;

import org.carabiner.infinitest.TestEvent;
import org.carabiner.infinitest.TestStatusListener;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a> 
 */
class EventNormalizer implements TestStatusListener
{
  private TestStatusListener listener;

  public EventNormalizer(TestStatusListener statusListener)
  {
    listener = statusListener;
  }
  
  public void testCaseSucceeded(final TestEvent event)
  {
    runThisOnTheEventThread(new Runnable()
    {
      public void run()
      {
        listener.testCaseSucceeded(event);        
      }      
    });    
  }

  public void testError(final TestEvent event)
  {
    runThisOnTheEventThread(new Runnable()
    {
      public void run()
      {
        listener.testError(event);        
      }      
    });    
  }

  public void testFailed(final TestEvent event)
  {
    runThisOnTheEventThread(new Runnable()
    {
      public void run()
      {
        listener.testError(event);        
      }      
    });    
  }

  public void testMethodSucceeded(final TestEvent event)
  {
    runThisOnTheEventThread(new Runnable()
    {
      public void run()
      {
        listener.testMethodSucceeded(event);        
      }      
    });    
  }

  public static void runThisOnTheEventThread(Runnable runnable)
  {
    if(SwingUtilities.isEventDispatchThread())
      runnable.run();
    else
      SwingUtilities.invokeLater(runnable);
  }
}
